package components
{
	import flash.display.Shape;
	import flash.net.NetConnection;
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Text;

	public class Model
	{
		static private var instance : Model;
		
		private var _data:ArrayCollection = new ArrayCollection([]);
		private var _itemId: Number = 0;
		
		static public function getInstance() : Model 
		{
			if ( instance == null ) 
			{
				instance = new Model( new Lock() );
			}
			
			return instance;
		}
		
		public function Model( lock : Lock ) : void 
		{
			if ( lock == null ) 
			{
				throw new Error( "Use getInstance() method instead" );
			}
		}
		
		public function set itemId(itemId: Number):void 
		{
			_itemId = itemId;
		}
		
		public function get itemId():Number 
		{
			return _itemId;
		}
		
		public function saveImage(imgData: Array):void
		{
			_data.addItem({id: "img" + _itemId, data: imgData});
			_itemId++;
			//_dashboard_so.setProperty("img" + itemId, imgData);
		}
		
		public function saveShape(shape: Shape):void
		{
			_data.addItem({id: "shp" + _itemId, data: Shape});
			_itemId++;
			//_dashboard_so.setProperty("shp" + itemId, Shape);
		}
		
		public function saveText(text: String):void
		{
			_data.addItem({id: "txt" + _itemId, data: text});
			_itemId++;
			//_dashboard_so.setProperty("txt" + itemId, text);
		}
		
		public function removeLastItem():void
		{
			if(_data.length > 0)
			{
				var itemId: String = _data.getItemAt( _data.length -1 ).id;
				_data.removeItemAt( _data.length -1 );
			}
		}
		
		public function removeItemById(itemId: String):void
		{
			for ( var index:String in _data )
			{
				if( _data.getItemAt(int(index)).id == itemId )
				{
					_data.removeItemAt(int(index));
				}
			}
		}
		
		public function get data():ArrayCollection
		{
			return _data;
		}
	}
}

internal class Lock{}
